<?php
/**
 * NOTICE OF LICENSE
 *
 * This source file is subject to the  Academic Free License (AFL 3.0)
 * that is available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to support@zprofess.com so we can send you a copy immediately.
 *
 * @category   	Prestashop module
 * @package		Customer Point
 * @version 	1.0 (March 2012)
 * @author 		xuanhoapro <xuanhoapro@gmail.com>
 * @copyright   Copyright (c) 2011 ZPROFESS. (http://www.zprofess.com)
 * @license    	http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

require_once(_PS_MODULE_DIR_.'zcustomerpoint/lib/zprofess/ZcustomerPointAdminTab.php');
include_once(dirname(__FILE__)."/classes/ZcustomerPointObject.php");
class ZadminCustomerPoint extends ZcustomerPointAdminTab {
	/**
	 * Construct
	 * @return <type>
	 */
	public function __construct()
	{
		global $cookie;
		$this->table = 'zcustomerpoint_accounts';
	 	$this->className = 'ZcustomerPointObject';
	 	$this->lang = false;
	 	$this->edit = true;
	 	$this->delete = false;
	 	$this->noAdd = true;
//	 	$this->requiredDatabase = true;
	 	
	 	$this->_select = 'zc.`username` as username_parent';
		$this->_join = 'LEFT JOIN `'._DB_PREFIX_.'customer` zc ON
		(zc.`id_customer` = a.`id_customer_parent`)';
		$this->fieldsDisplay = array(
			'id_zcustomerpoint_accounts' => array('title' => $this->l('ID'), 'align' => 'center', 'width' => 25),
			'username' => array('title' => $this->l('Tên đăng nhập'), 'width' => 80, 'filter_key' => 'a!username'),
			'name' => array('title' => $this->l('Họ tên'), 'width' => 100, 'filter_key' => 'a!name'),
			'points_current' => array('title' => $this->l('Points'), 'align' => 'center', 'width' => 100),
			'username_parent' => array('title' => $this->l('Người giới thiệu'), 'width' => 120, 'maxlength' => 19),
			'block'=>array('title'=>$this->l('Block'),'width'=>25,'align'=>'center','type'=>'bool','active' => 'block','orderdy'=>false),
		);
		parent::__construct();
	}
	
	/**
	 * Display From
	 * @param unknown_type $isMainTab
	 */
	public function displayForm ($isMainTab = true){
		global $currentIndex, $cookie;
		parent::displayForm();
		if (!($obj = $this->loadObject(true)))
			return;

		$customerParent = Db::getInstance()->getRow('SELECT * FROM '._DB_PREFIX_.'customer WHERE id_customer ='.$obj->id_customer_parent);
		echo '<div id="emailevent"><form action="'.$currentIndex.'&submitAdd'.$this->table.'=1&token='.$this->token.'" method="post">
			'.($obj->id ? '<input type="hidden" name="id_'.$this->table.'" value="'.$obj->id.'" /> ' : '');
		echo '
			<fieldset style="width:900px;">
				<legend>'.$this->l('Customer points').'</legend>
				<label>'.$this->l('Tên đăng nhập').'</label>
				<div class="margin-form">
					<input type="text" style="color: #7F7F7F;" size="40" name="full_name" value="'.(($obj->username) ? $obj->username : 'No name').'" DISABLED /><sup></sup>
				</div>
				<div class="clear"></div>
				<label>'.$this->l('Người giới thiệu').':</label>
				<div class="margin-form">
					<input type="text" style="color: #7F7F7F;" size="40" name="parent_email" value="'.($customerParent['username'] ? $customerParent['username'] : '--').'" DISABLED /><sup></sup>
				</div>';
		echo $this->addElement ('text',array ('label'=>'Points','name'=>'points_current', 'note'=>'','size'=>'40'));
		echo $this->addElement ('yesno',array ('label'=>'Block','name'=>'block'));
		echo'	<div class="margin-form">
					<input type="submit" class="button" name="submitAdd'.$this->table.'" value="'.$this->l('Save').'"/>
				</div>
				<div class="small"><sup>*</sup> '.$this->l('Required field').'</div>
				<div class="clear"></div>
			</fieldset>
		</form></div>';
		
		
	}
	
	public function postProcess($token = NULL)
	{
		global $currentIndex;
		if ((isset($_GET['block'.$this->table]) OR isset($_GET['block'])) AND Tools::getValue($this->identifier))
		{
			if ($this->tabAccess['edit'] === '1')
			{
				if (Validate::isLoadedObject($object = $this->loadObject(true)))
				{
					if ($object->toggleBlock())
						Tools::redirectAdmin($currentIndex.'&conf=4'.((($id_category = (int)(Tools::getValue('id_category'))) AND Tools::getValue('id_product')) ? '&id_category='.$id_category : '').'&token='.$this->token);
					else
						$this->_errors[] = Tools::displayError('An error occurred while updating status.');
				}
				else
					$this->_errors[] = Tools::displayError('An error occurred while updating status for object.').' <b>'.$this->table.'</b> '.Tools::displayError('(cannot load object)');
			}
			else
				$this->_errors[] = Tools::displayError('You do not have permission to edit here.');
		}
		
		parent::postProcess ();
	}
	
}
